﻿#include "NCTDialogMultiple.h"
#include "ui_NCTDialogMultiple.h"
#include "NCTDialogMultipleOwned.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTMultipleNoise.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTLayUp.h"

#include <QMessageBox>
#include <QComboBox>

namespace GUI
{
    NCTDialogMultiple::NCTDialogMultiple(PhysicsDlgExecType type, int nctid/*=-1*/, QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type, parent)
    {
        _ui = new Ui::NCTDialogMultiple;
        _ui->setupUi(this);
        //获取声学包对象
        if (_nctMgr && nctid > 0)
            _nct = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_nctMgr->getDataByID(nctid));

        this->init();
    }

    NCTDialogMultiple::~NCTDialogMultiple()
    {
        if (_ui) delete _ui;
    }

    void NCTDialogMultiple::on_pushButton_OK_clicked()
    {
        if (!this->checkInput()) return;
        //要处理的对象
        Acoustics::FITKAcousticsNCTMultipleNoise* mult = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_nct);
        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            mult = new Acoustics::FITKAcousticsNCTMultipleNoise;
            _nctMgr->appendDataObj(mult);
        }
        if (!mult) return;
        //保存数据  
        mult->clear();
        mult->setDataObjectName(_ui->lineEdit_name->text());
        for (int i = 0; i < _dataList.length(); i++)
        {
            if (!_dataList[i]) continue;
            mult->insertDataObj(i, _dataList[i]);
        }
        this->accept();
    }

    void NCTDialogMultiple::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void NCTDialogMultiple::on_pushButton_delete_clicked()
    {
        //获取期选中的行
        int row = _ui->tableWidget->currentRow();
        if (row < 0)return;
        //删除行数据
        _ui->tableWidget->removeRow(row);
        //删除数据
        _dataList.removeAt(row - 1);
        refreshCoverageArea();
        refreshTableWidget();
    }

    void NCTDialogMultiple::on_pushButton_edit_clicked()
    {
        int row = _ui->tableWidget->currentRow();
        if (row < 0 || row >= _dataList.size())return;
        auto data = _dataList[row];
        if (!data)return;
        GUI::NCTDialogMultipleOwned dlog(data);
        if (QDialog::Accepted == dlog.exec()) {
            refreshCoverageArea();
            refreshTableWidget();
        }
    }

    void NCTDialogMultiple::on_pushButton_add_clicked()
    {
        GUI::NCTDialogMultipleOwned dlog;  
        if (QDialog::Accepted == dlog.exec()) {
            auto newData = dlog.getData();
            if (!newData)return;
            _dataList.append(newData);
            refreshCoverageArea();
            refreshTableWidget();
        }
    }

    void NCTDialogMultiple::on_pushButton_copy_clicked()
    {
        int row = _ui->tableWidget->currentRow();
        if (row < 0 || row >= _dataList.size())return;
        auto data = _dataList[row];
        if (!data)return;
        auto newData = new Acoustics::FITKAcousticsNCTMultipleNoiseWeight();
        newData->setNCTID(data->getNCTID());
        newData->setCoverage(data->getCoverage());

        GUI::NCTDialogMultipleOwned dlog(newData);
        if (QDialog::Accepted == dlog.exec()) {
            _dataList.append(newData);
            refreshCoverageArea();
            refreshTableWidget();
        }
    }

    void NCTDialogMultiple::init()
    {
        if (_nctMgr == nullptr) return;
        //不可编辑，隐藏行号
        _ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        _ui->tableWidget->verticalHeader()->setVisible(false);

        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _nctMgr->checkName("New Treatment Lay-up");
            _ui->lineEdit_name->setText(name);
            _ui->tableWidget->resizeColumnsToContents();
        }
        Acoustics::FITKAcousticsNCTMultipleNoise* mult = dynamic_cast<Acoustics::FITKAcousticsNCTMultipleNoise*>(_nct);
        if (!mult) return;

        _ui->lineEdit_name->setText(mult->getDataObjectName());
        int dataCount = mult->getDataCount();
        _ui->tableWidget->setRowCount(dataCount);
        for (int i = 0; i < dataCount; i++)
        {
            Acoustics::FITKAcousticsNCTMultipleNoiseWeight* data = mult->getDataByIndex(i);
            if (!data) continue;
            //复制一份数据
            auto newData = new Acoustics::FITKAcousticsNCTMultipleNoiseWeight();
            newData->setNCTID(data->getNCTID());
            newData->setCoverage(data->getCoverage());
            _dataList.append(newData);
        }
        //填充表格数据
        refreshTableWidget();
        refreshCoverageArea();
        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(mult->getDataObjectName());
            _ui->lineEdit_name->setText(text);
        }

    }
    bool NCTDialogMultiple::checkInput()
    {
        double cov = _ui->lineEdit_totalCoverage->text().toDouble();
        if (cov>100)
        {
            QMessageBox::critical(nullptr, tr("Critical"), tr("The coverage rate cannot exceed 100."));
            return false;
        }
        if (_nctMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_name->text().trimmed();
        if (text != "") {
            QString newText = _nctMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (_nctMgr->getDataByName(text) == _nct) return true;
        }
        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }

    void NCTDialogMultiple::refreshTableWidget()
    {
        if (!_nctMgr)return;
        //清空
        _ui->tableWidget->setRowCount(0);
        int rowCount = _dataList.length();
        _ui->tableWidget->setRowCount(rowCount);
        for (int i = 0; i < rowCount; i++)
        {
            Acoustics::FITKAcousticsNCTMultipleNoiseWeight* w = _dataList.at(i);
            if(w == nullptr) continue;

            _ui->tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i+1)) );

            _ui->tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(w->getCoverage() * 100)));
            Acoustics::FITKAcousticsAbsNCT * nctData = _nctMgr->getDataByID(w->getNCTID());
            if(nctData == nullptr) continue;
            if (nctData->getNCTType() == Acoustics::FITKAcousticsAbsNCT::NCTType::UserDefTreatment) {
                _ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tr("User-defined")));
            }
            else if (nctData->getNCTType() == Acoustics::FITKAcousticsAbsNCT::NCTType::TreatmentLayUp) {
                _ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tr("Layup")));
            }
            _ui->tableWidget->setItem(i, 2, new QTableWidgetItem(nctData->getDataObjectName()));
        }
        //自适应宽度
        _ui->tableWidget->resizeColumnsToContents();
    }

    void NCTDialogMultiple::refreshCoverageArea()
    {
        double coverage = 0.0;
        double massPerUnitArea = 0.0;
        for (int i = 0; i < _dataList.length(); i++)
        {
            if (!_dataList[i])continue;
            coverage = coverage + _dataList[i]->getCoverage();

            int id = _dataList[i]->getNCTID();
            Acoustics::FITKAcousticsAbsNCT* nctData = _nctMgr->getDataByID(id);
            if (!nctData)continue;
            if (nctData->getNCTType() == Acoustics::FITKAcousticsAbsNCT::NCTType::UserDefTreatment) {
                Acoustics::FITKAcousticsNCTUserDefined* glData = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>(nctData);
                massPerUnitArea = massPerUnitArea + glData->getMassPerUnitArea();
            }
            else if (nctData->getNCTType() == Acoustics::FITKAcousticsAbsNCT::NCTType::TreatmentLayUp) {
                Acoustics::FITKAcousticsNCTLayUp* glData = dynamic_cast<Acoustics::FITKAcousticsNCTLayUp*>(nctData);
                massPerUnitArea = massPerUnitArea + glData->getMassPerUnitArea();
            }
        }
        _ui->lineEdit_totalMassUnitArea->setText(QString::number(massPerUnitArea));
        _ui->lineEdit_totalCoverage->setText(QString::number(coverage*100 ));
    }

}